package org.jash.shop2112.user

import android.os.Bundle
import android.util.Log
import android.view.View
import android.widget.Button
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import androidx.coordinatorlayout.widget.CoordinatorLayout
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import androidx.lifecycle.ViewModelProvider
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.google.android.material.behavior.SwipeDismissBehavior
import com.hyphenate.chat.ChatClient
import com.hyphenate.helpdesk.callback.Callback
import com.tencent.mmkv.MMKV
import org.jash.shop2112.R
import org.jash.shop2112.databinding.ActivityLoginBinding
import org.jash.shop2112.mvvm.BaseActivity
import org.jash.shop2112.utils.log
import org.jash.shop2112.utils.user

@Route(path = "/shop/login")
class LoginActivity : BaseActivity<ActivityLoginBinding, LoginViewModel>() {
    val map by lazy { User(kv.decodeString("username") ?: "", kv.decodeString("password") ?: "") }
    val kv by lazy { MMKV.defaultMMKV() }
    override fun initData() {
        viewModel.userLiveData.observe(this) {
//            ChatClient.getInstance().logout(true, object :Callback {
//                override fun onSuccess() {
//                }
//
//                override fun onError(code: Int, error: String?) {
//                }
//
//                override fun onProgress(progress: Int, status: String?) {
//                }
//            })
            ChatClient.getInstance().login("user1", "password", object : Callback {
                override fun onSuccess() {
                    this@LoginActivity.log("登录成功")
                }

                override fun onError(code: Int, error: String?) {
                    this@LoginActivity.log("登录失败: $error")
                }

                override fun onProgress(progress: Int, status: String?) {
                    this@LoginActivity.log("登录状态 $status")
                }
            })
            user = it
            kv.encode("username", map.username)
            kv.encode("password", map.password)
            ARouter.getInstance()
                .build("/shop/main")
                .navigation()
            finish()
        }
    }

    override fun initView() {
        binding.user = map
        binding.login.setOnClickListener {
            viewModel.login(map)
        }
        binding.register.setOnClickListener {
            ARouter.getInstance()
                .build("/shop/register")
                .navigation()
        }
    }

}